package cn.lanqiao.bigDate.service.sys.impl;


import cn.lanqiao.bigDate.entity.sys.*;
import cn.lanqiao.bigDate.mapper.sys.*;
import cn.lanqiao.bigDate.reslut.TreeNode;
import cn.lanqiao.bigDate.service.sys.SysRoleService;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;

@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Resource
    private SysRoleMapper sysRoleMapper;

    @Resource
    private SysMenuMapper sysMenuMapper;
    @Resource
    private SysRoleMenuMapper sysRoleMenuMapper;


    /**
     * 分页查询
     * @param page  分页
     * @param searchLike    条件查询
     * @return
     */
    @Override
    public IPage<SysRole> findByPage(Page<SysRole> page,String searchLike) {
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        queryWrapper.like("id",searchLike).or().like("role_name",searchLike).or().like("create_by",searchLike).or().like("update_by",searchLike).or().like("remark",searchLike);
        Page<SysRole> sysRolePage = sysRoleMapper.selectPage(page, queryWrapper);
        return sysRolePage;
    }

    @Override
    public List<SysRole> findAll() {
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        List<SysRole> sysRoles = sysRoleMapper.selectList(null);
        return sysRoles;
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public SysRole findSysRoleOne(Long id) {
        QueryWrapper<SysRole> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        SysRole sysRole = sysRoleMapper.selectOne(wrapper);
        return sysRole;
    }


    /**
     * 修改角色信息
     * @param sysRole
     * @return
     */
    @Override
    public Boolean updateSysRole(SysRole sysRole,String[] split) {
        if (sysRole !=null && sysRole.getId()!=null){
            Long id = sysRole.getId();
            QueryWrapper<SysRoleMenu> wrapper = new QueryWrapper<>();
            wrapper.eq("role_id",id);
            int delete = sysRoleMenuMapper.delete(wrapper);
            System.out.println("删除记录数："+delete);
            //查询源对象
            SysRole sysRoleOne = findSysRoleOne(id);
            sysRoleOne.setCreateBy(sysRole.getCreateBy());
            sysRoleOne.setRemark(sysRole.getRemark());
            sysRoleOne.setRoleName(sysRole.getRoleName());
            sysRoleOne.setUpdateBy(sysRole.getUpdateBy());
            sysRoleOne.setUpdateTime(LocalDateTime.now());
            int i = sysRoleMapper.updateById(sysRoleOne);
            if (i==1 && split[0]!=""){
                for (String menuId:split) {
                    SysRoleMenu srm = new SysRoleMenu(Long.valueOf(menuId),id);
                    int i1 = sysRoleMenuMapper.insert(srm);
                }
            }
            return true;
        }
        return false;
    }

    /**
     *添加角色信息并给角色根据权限树添加权限
     * @param sysRole
     * @return
     */
    @Override
    public Boolean saveSysRole(SysRole sysRole, String[] split) {
        sysRole.setCreateTime(LocalDateTime.now());
        int insert = sysRoleMapper.insert(sysRole);
        if (insert == 1 && split[0]!="") {
            Long id = sysRole.getId();
                for (String menuId : split) {
                    SysRoleMenu srm = new SysRoleMenu(Long.valueOf(menuId), id);
//                    System.out.println(srm);
                    int i = sysRoleMenuMapper.insert(srm);
                }
                return true;
        }
        return true;
    }

    /**
     *添加角色信息
     * @param sysRole
     * @return
     */
    @Override
    public Boolean saveSysRole(SysRole sysRole) {
        int insert = sysRoleMapper.insert(sysRole);
        if (insert>0){
            return true;
        }
        return false;
    }



    /**
     * 删除角色信息
     * @param ids 格式  id1,id2,id3
     * @return
     */
    @Override
    public Boolean deleteSysRole(String ids) {
        //判断字符串是否为空
        if(!StringUtils.isEmpty(ids)){
            String[] id = ids.split(",");
            List<String> strings = Arrays.asList(id);
            int i = sysRoleMapper.deleteBatchIds(strings);
            if (i>0){
                return true;
            }
        }
        return false;
    }

    @Override
    public List<TreeNode> findAllSysMenu(SysMenu menu, String topName) {
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        //获取所有菜单
        List<SysMenu> sysMenus = sysMenuMapper.selectList(wrapper);
        //将菜单转换成树节点
        List<TreeNode> treeNodes = TreeNode.convertTreeNodes(sysMenus, topName);
        return treeNodes;
    }

    @Override
    public SysMenu findSysMenuOne(Long id) {
        //创建一个QueryWrapper的对象
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        SysMenu sysMenu = sysMenuMapper.selectOne(wrapper);
        return sysMenu;
    }



}
